package tms;

import java.util.List;

import tms.model.Account;
import tms.model.Aircraft;
import tms.model.Booking;
import tms.model.Destination;
import tms.model.Flight;
import tms.service.ServiceHandler;

public class DemoService {

	public static void main(String[] args) {

		System.out.println("Load one destination");
		Destination d = ServiceHandler.getFirstDestination("destName like '%paris%'");
		System.out.println("\t\tDestination: " + d.getDestName() + " | Code : "+ d.getDestCode());
		System.out.println("=========================================");

		System.out.println("Load a list of destination");
		List<Destination> ls = ServiceHandler.getListDestination("");
		for (Destination obj : ls) {
			System.out.println("\t\tDestination: " + obj.getDestName() + " | Code : "+ obj.getDestCode());
		}
		System.out.println("=========================================");

//		//Following functions show loading of all entites
//		System.out.println("Load one Account");
//		Account a = ServiceHandler.getFirstAccount("email like '%admin%'");
//		System.out.println("\t\tFullname: " + a.getFullName() + " | Email: " + a.getEmail());
//
//		System.out.println("Load a list of account");
//		List<Account> lsA = ServiceHandler.getListAccount("");
//		for (Account obj : lsA) {
//			System.out.println("\t\tFullname: " + obj.getFullName() + " | Email: " + obj.getEmail());
//		}
//
//		System.out.println("=========================================");
//
//		System.out.println("Load one Booking");
//		Booking b = ServiceHandler.getFirstBooking("ticketnumber like '%TKN%'");
//		System.out.println("\t\tFlight name:" + b.getFlight().getFlightName());
//		System.out.println("\t\tTicket number:" + b.getTicketNumber());
//		System.out.println("=========================================");
//
		System.out.println("Load a list of Booking");
		List<Booking> lsB = ServiceHandler.getListBooking("");
		for (Booking obj : lsB) {
			System.out.println("\t\tFlight name: " + obj.getFlight().getFlightName());
			System.out.println("\t\tTicket number: " + obj.getTicketNumber());
		}
//
//		System.out.println("=========================================");
//
//		System.out.println("Load one Flight");
//		Flight f = ServiceHandler.getFirstFlight("flightname like '%F002%'");
//		System.out.println("\t\tFlight name: " + f.getFlightName());
//		System.out.println("\t\tFrom: " + f.getDeparting().getDestName() + " To : " + f.getArriving().getDestName());
//		System.out.println("=========================================");
//
//		System.out.println("Load a list of Flight");
//		List<Flight> lsF = ServiceHandler.getListFlight("");
//		for (Flight obj : lsF) {
//			System.out.println("\t\tFlight name: " + obj.getFlightName());
//			System.out.println("\t\tFrom: " + obj.getDeparting().getDestName() + " To : " + obj.getArriving().getDestName());
//		}
//		System.out.println("=========================================");
//
//		System.out.println("Load one Aircraft");
//		Aircraft ar = ServiceHandler.getFirstAircraft("name like '%360%'");
//		System.out.println("\t\tAircraft name : " + ar.getName() + " | Code : " + ar.getCode());
//		System.out.println("=========================================");
//
//		System.out.println("Load a list of Aircraft");
//		List<Aircraft> lsAr = ServiceHandler.getListAircraft("");
//		for (Aircraft obj : lsAr) {
//			System.out.println("\t\tAircraft name : " + obj.getName() + " | Code : " + obj.getCode());
//		}

		System.out.println("=========================================");
		System.out.println("Create a new Destination");
		Destination outerSpace = new Destination(); 
		outerSpace.setDestName("Outer Space");
		outerSpace.setDestCode("OS");
		outerSpace.setActive(true);
		System.out.println("\tSave new Destination [" + outerSpace.getDestName() + "]");
		System.out.println("\tSuccess: \t" + ServiceHandler.saveDestination(outerSpace));
		
		System.out.println("Update a Destination");
		//on creation Id is < 0 so we need to reload the object from database to obtain its Id
		outerSpace = ServiceHandler.getFirstDestination("DestName like '%space%'");
		String oldName = outerSpace.getDestName();
		if (outerSpace != null && outerSpace.getId() > 0)
		{
			outerSpace.setDestName("Some where outside the Earth");
			System.out.println("\tUpdate Destination [" + oldName + "] to " + "[" + outerSpace.getDestName()+"]");
			System.out.println("\tSuccess: \t" + ServiceHandler.saveDestination(outerSpace));
		} else
			System.out.println("Cannot find the corresponding Destination");
		
		System.out.println("Delete a Destination");
		System.out.println("\tDestination [" + outerSpace.getDestName() + "] will be removed");
		System.out.println("\tSuccess: \t" + ServiceHandler.deleteDestination(outerSpace));
	}

}
